// ----------------------------------------
// Filename: Semaphore.h
// Description: The header file for the Semaphore
// Author: Jon Maloney
// Date: 9-May-2012
//

#ifndef BINARY_SEMAPHORE_H
#define BINARY_SEMAPHORE_H

//************************************************************
//                    Include .h files
//************************************************************
#include "microRTOS.h"
#include "Linked_List.h"
//************************************************************
//                    Include external references
//************************************************************

//************************************************************
//                    Define Statements
//************************************************************

//************************************************************
//                    Struct, Union, enum Statements
//************************************************************

typedef struct
{
	PRIORITY taskUsingSemPriority;
	linkedList_Handle waitingTaskNode;
	linkedList_Handle taskUsingSemNode;		
}bsem_t;

typedef bsem_t * bsem_Handle;

//************************************************************
//                    Global variables and constants
//************************************************************

//************************************************************
//                    Prototype Global Functions
//************************************************************

// ----------------------------------------
// sem_Create
// 
// Creates a new semaphore
// Input:
//	pdTRUE: 	During creation semaphore_Wait is called and the 
//					task creating the semaphore is waiting on the semaphroe
//	pdFALSE:	There is no tasks waiting on the semaphore.
// Output:
//  sem_Handle: A pointer to the semaphore created.
//					If the pointer is NULL then the semaphore
//					was not created.
// Conditions:
// 	none
bsem_Handle sem_Create( BOOL initVal );

// ----------------------------------------
// sem_Delete
// 
// Deletes a semaphore
// Input:
//	Semaphore: 	A handle to the semaphore that is to be deleted.
// Output:
//  none
// Conditions:
// 	none
void sem_Delete(bsem_Handle Semaphore);

// ----------------------------------------
// Semaphore_Take
// 
// Gives a task access to a semaphore if the
//	semaphore is available
//	
// Input:
// 	Semaphore: 	A handle to the semaphore that you are trying to take.
//	MaxWaitTime:The maximum time that the thread will wait on the semaphore. 
// Output:
//  pdTRUE: if a semaphore is obtained.
//	pdFALSE:if the semaphore cannot be obtained.
// Conditions:
// 	none
BOOL Semaphore_Wait(bsem_Handle Semaphore, kernelTickType MaxWaitTime);

// ----------------------------------------
// Semaphore_Signal
// 
// Signals any thread waiting on the semaphore
//	that the semaphore is now available
// Input:
// 	Semaphore: 	A handle to the semaphore that you are trying to release a token to.
// Output:
//	none
// Conditions:
// 	none
void Semaphore_Signal(bsem_Handle Sem);

#endif
